require 'sinatra'
require 'json'
require 'ruby-hackernews'

require 'rhn/util'
require 'rhn/apigen'
require 'rhn/controllers'

include ApiGen


class RHN < Sinatra::Base
  get '/api/' do
    begin
      decoded = decode_req params[:r]
      result = {:result => from_dsl(decoded)}
    rescue AccessDenied => exc
      status 550
      result = {:error => exc.to_s}
    rescue NotFound => exc
      status 404
      result = {:error => exc.to_s}
    rescue JSON::ParserError
      status 400
      result = {:error => "The parameter you passed couldn't be parsed"}
    end

    content_type :json
    result.to_json
  end
end
